package com.picvik.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.picvik.model.DiscussionCommentsModel;
import com.picvik.model.DiscussionCommentsUtil;
import com.picvik.model.DiscussionModel;
import com.picvik.model.DiscussionRecommendationEngine;
import com.picvik.model.DiscussionUtil;
import com.picvik.model.TagDiscussionModel;
import com.picvik.model.TagDiscussionUtil;
import com.picvik.model.TagModel;
import com.picvik.model.TagUtil;
import com.picvik.model.UserModel;

public class DiscussionAction extends ActionSupport {
	private String title;
	private String description;
	private String tags;
	private Integer discID;
	
	private Integer discussionID;
	private String comment;

	public Integer getDiscussionID() {
		return discussionID;
	}

	public void setDiscussionID(Integer discussionID) {
		this.discussionID = discussionID;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public Integer getDiscID() {
		return discID;
	}

	public void setDiscID(Integer discID) {
		this.discID = discID;
	}

	DiscussionModel discmodel = new DiscussionModel();
	TagDiscussionModel tagdiscmodel = new TagDiscussionModel();
	TagModel tagmodel = new TagModel();
	ArrayList<DiscussionModel> viewdiscmodel = new ArrayList<DiscussionModel>();
	ArrayList<DiscussionModel> recdiscmodel = new ArrayList<DiscussionModel>();
	ArrayList<DiscussionModel> recdiscmodeltagbased = new ArrayList<DiscussionModel>();
	ArrayList<DiscussionCommentsModel> disccomment = new ArrayList<DiscussionCommentsModel>();
	
	public ArrayList<DiscussionCommentsModel> getDisccomment() {
		return disccomment;
	}

	public void setDisccomment(ArrayList<DiscussionCommentsModel> disccomment) {
		this.disccomment = disccomment;
	}

	public ArrayList<DiscussionModel> getRecdiscmodeltagbased() {
		return recdiscmodeltagbased;
	}

	public void setRecdiscmodeltagbased(
			ArrayList<DiscussionModel> recdiscmodeltagbased) {
		this.recdiscmodeltagbased = recdiscmodeltagbased;
	}

	DiscussionModel descdetails = new DiscussionModel();


	public DiscussionModel getDescdetails() {
		return descdetails;
	}

	public void setDescdetails(DiscussionModel descdetails) {
		this.descdetails = descdetails;
	}

	public ArrayList<DiscussionModel> getRecdiscmodel() {
		return recdiscmodel;
	}

	public void setRecdiscmodel(ArrayList<DiscussionModel> recdiscmodel) {
		this.recdiscmodel = recdiscmodel;
	}

	public ArrayList<DiscussionModel> getViewdiscmodel() {
		return viewdiscmodel;
	}

	public void setViewdiscmodel(ArrayList<DiscussionModel> viewdiscmodel) {
		this.viewdiscmodel = viewdiscmodel;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getTags() {
		return tags;
	}

	public void setTags(String tags) {
		this.tags = tags;
	}

	public String startnewdisc() {
		return SUCCESS;
	}

	public String createnewdisc() {
		DiscussionModel discmodel = new DiscussionModel();
		TagDiscussionModel tagdiscmodel = new TagDiscussionModel();
		TagModel tagmodel = new TagModel();

		DiscussionUtil du = new DiscussionUtil();
		TagUtil tagu = new TagUtil();
		TagDiscussionUtil tdu = new TagDiscussionUtil();

		Map sess = (Map) ActionContext.getContext().get("session");
		Integer uid = (Integer) sess.get("uid");
		String userName = UserModel.getUserNameFromID(uid);

		discmodel.setTitle(title);
		discmodel.setDescription(description);
		discmodel.setOwner_id(uid);
		discmodel.setOwner_name(userName);
		int recentdiscID = du.insertDiscussion(discmodel);
		int recenttagID;
		String[] taglist = tags.split(",");
		if (taglist.length == 0) {
			recenttagID = tagu.insertNewTag("General");
			tagdiscmodel.setDiscussion_id(recentdiscID);
			tagdiscmodel.setTag_id(recenttagID);
			tdu.addNewTagDiscussion(tagdiscmodel);
		} else {
				for (String t:taglist) {
					recenttagID = tagu.insertNewTag(t);
					tagdiscmodel.setDiscussion_id(recentdiscID);
					tagdiscmodel.setTag_id(recenttagID);
					tdu.addNewTagDiscussion(tagdiscmodel);
				}
		}

		
		return SUCCESS;
	}

	public String viewdisc() {
		
		DiscussionRecommendationEngine dre = new DiscussionRecommendationEngine();
		DiscussionUtil du = new DiscussionUtil();
		viewdiscmodel = du.getRecentFiftyDiscussion();
		//recdiscmodel = dre.getRecommendedDiscussions(new String[] {"General"});
		recdiscmodel = dre.getRecommendedDiscussions(null); // Passing NULL will fetch latest fifty general discussion
		System.out.println(recdiscmodel.size());
		return SUCCESS;
	}
	
	public String discthreadview() {
		System.out.println(discID);
		DiscussionUtil du = new DiscussionUtil();
		TagUtil tg = new TagUtil();
		DiscussionRecommendationEngine dre = new DiscussionRecommendationEngine();
		descdetails= du.getDiscussionThreadByID(discID);
		
		TagDiscussionUtil tgu = new TagDiscussionUtil();
		ArrayList<Integer> tagsids =  new ArrayList<Integer>();
		List<String> tagsnames = new ArrayList<String>();
		tagsids = tgu.getAllTagIdsWithDiscussionId(discID);
		for (int i=0; i< tagsids.size(); i++) {
			tagsnames.add(tg.getTagName(tagsids.get(i)));
		}
		String[] tagarr = new String[ tagsnames.size() ];
		String[] tagarrfinal = tagsnames.toArray( tagarr );
		recdiscmodeltagbased = dre.getRecommendedDiscussions(tagarrfinal);
		
		//get comments
		DiscussionCommentsUtil dcm = new DiscussionCommentsUtil();
		disccomment = dcm.getDiscussionComment(discID);
		
		return SUCCESS;
	}
	
	public String disccomment() {
		DiscussionCommentsUtil dcu = new DiscussionCommentsUtil();
		DiscussionCommentsModel dcm = new DiscussionCommentsModel();
		
		Map sess = (Map) ActionContext.getContext().get("session");
		Integer uid = (Integer) sess.get("uid");
		
		
		dcm.setComment(comment);
		dcm.setDiscussion_id(discussionID);
		dcm.setOwner_id(uid);
		dcu.addNewDiscussionComment(dcm);
		return SUCCESS;
	}
}
